package com.dwf.dominion;

import java.util.Vector;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ActivityCardList extends Activity {

	Vector<Card> cardList;
	RuleList ruleList;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);
		
		//TODO: restore last rule set used, hard coding for now
		setNewRuleList("Lunch Group", RuleList.RuleListType.RANDOM);
	}

	private void setNewRuleList(String name, RuleList.RuleListType type ) {
		ruleList = new RuleList(name, type);

		cardList = ruleList.getNewList(null);
		ListView card_list = (ListView) findViewById(R.id.card_list);
		
		CardListArrayAdapter adapter = new CardListArrayAdapter(this,
				R.layout.card_list_layout, cardList); 
		card_list.setAdapter(adapter);
		
		this.setTitle("Card Selector: " + name);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.card_list_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.quit:
			System.exit(0);
			break;
		case R.id.rules:
			Intent rulesIntent = new Intent(this,RuleSets.class);
			startActivityForResult(rulesIntent, 0);
		default:
			break;
		}
		
		return super.onOptionsItemSelected(item);
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		Bundle bundle = data.getExtras();
		String ruleListName = bundle.getString("name");
		
		//TODO: should search for the rule list file here, for now hardcode the type
		setNewRuleList(ruleListName,RuleList.RuleListType.RANDOM);
		this.setTitle("Card Selector: " + ruleListName);
	}
	public void onClickReplaceCards(View v)
	{
		updateCardRequirements();
		
		cardList = ruleList.getNewList(cardList);
		
		ListView card_list = (ListView) findViewById(R.id.card_list);
		card_list.removeAllViewsInLayout();
		card_list.setAdapter(new CardListArrayAdapter(this,
					R.layout.card_list_layout, cardList));

	}
	
	public void onClickShuffleCards(View v) {
		updateCardRequirements();
		
		cardList = ruleList.getNewList(null);
		ListView card_list = (ListView) findViewById(R.id.card_list);
		card_list.removeAllViewsInLayout();
		card_list.setAdapter(new CardListArrayAdapter(this,
					R.layout.card_list_layout, cardList));
	}

	private void updateCardRequirements() {
		Vector<Card> clone = new Vector<Card>();
		clone.addAll(cardList);
				
		//Have to loop on the clone, otherwise we will be
		//  removing on the same vector we are looping on
		//  and this can cause run time errors (from my experience)
		for(Card c: clone) {
			if(c.state == Card.State.TEMP_BAN) {
				ruleList.addTempBannedCard(c);
				cardList.remove(c);
			}
		}
				
		for(Card c: cardList) {
			if(c.state == Card.State.TEMP_LOCK) {
				ruleList.addTempRequiredCard(c);
			}
		}
		
		for(Card c: cardList) {
			if(c.state == Card.State.NORMAL) {
				ruleList.removeTempRequiredCard(c);
			}
		}
	}
	
	public void onClickCardList(View v) {
		LinearLayout layout = (LinearLayout) v;
		
		Log.i("this","card clicked");
		
		//layout.setBackgroundResource(R.color.card_temp_lock);
		TextView cardName = (TextView)layout.getChildAt(2);
		Card thisCard = null;
		
		for(Card c: cardList)
		{
			String s = cardName.getText().toString();
			if(c.name.equals(s)) {
				thisCard = c;
				break;
			}
		}
	
		if(thisCard != null) {
			switch(thisCard.state) {
			case NORMAL:
				thisCard.state = Card.State.TEMP_BAN;
				break;
			case TEMP_LOCK:
				thisCard.state = Card.State.NORMAL;
				break;
			case TEMP_BAN:
				thisCard.state = Card.State.TEMP_LOCK;
				break;
			default:
				break;
			}
		}

		setCardBackgroundColor(thisCard, layout);

	}
	
	private void setCardBackgroundColor(Card card, LinearLayout layout) {
		if((card != null) && (layout != null)) {
			switch(card.state) {
			case NORMAL:
				layout.setBackgroundDrawable(null);
				break;
			case TEMP_LOCK:
				layout.setBackgroundResource(R.color.card_temp_lock);
				break;
			case TEMP_BAN:
				layout.setBackgroundResource(R.color.card_temp_ban);
				break;
			default:
				break;
			}
		}
	}
	
	private OnLongClickListener longClickListener = new OnLongClickListener() {

		public boolean onLongClick(View v) {
			Log.i("longClickListener","Long click detected.");
			return true;
		}
		
	};

}